package com.solomon.travel.common.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.solomon.travel.common.base.controller.BaseController;
import com.solomon.travel.common.custom.po.DiningRoomCustom;
import com.solomon.travel.common.custom.po.HotelCustom;
import com.solomon.travel.common.custom.po.MettingRoomCustom;
import com.solomon.travel.common.model.ApiResponse;
import com.solomon.travel.common.model.Page;
import com.solomon.travel.common.po.Hotel;
import com.solomon.travel.common.po.Image;
import com.solomon.travel.common.po.Protocal;
import com.solomon.travel.common.po.RoomPrice;
import com.solomon.travel.common.service.HotelService;
import com.solomon.travel.common.service.ImageService;
import com.solomon.travel.common.util.ConstantUtils;
import com.solomon.travel.common.util.IdUtils;
import com.solomon.travel.common.util.PageUtils;

@RestController
@RequestMapping("/web/hotel")
public class HotelController extends BaseController {
	@Resource
	private HotelService hotelService;
	
	@Resource
	private ImageService imageService;
	
	@RequestMapping("/get")
	public ApiResponse getHotel() throws Exception{
		HotelCustom hotelCustom = hotelService.getHotelByUser(currentUser().getObjectId());
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, hotelCustom);
	}
	
	@RequestMapping("/update")
	public ApiResponse updateHotel(Hotel hotel,@RequestParam MultipartFile headerImage) throws Exception{
		logger.info("hotel--->"+JSON.toJSONString(hotel));
		if (!headerImage.isEmpty()) {
			String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			 String originalName = headerImage.getOriginalFilename();
			 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
			 String path = firstKey + "/hotel/" + datePath + "/" + fileName;
			 ossClient.putObject(bucketName, path, headerImage.getInputStream());
			 hotel.setImageUrl(ossurl+path);
		}
		hotelService.update(hotel);
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK,hotel);
	}
	
	@RequestMapping("/delete_old_image")
	public ApiResponse delete_old_image(String organization_id) {
		imageService.delete_old_image(organization_id);
		return defaultResponse();
	}
	
	@RequestMapping("/dining")
	public ApiResponse getDiningRoom(String hotelId) {
		DiningRoomCustom diningRoomCustom = hotelService.getDiningRoomByHotel(hotelId);
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, diningRoomCustom);
	}
	
	@RequestMapping("/metting")
	public ApiResponse getMettingRoom(String hotelId) {
		MettingRoomCustom diningRoomCustom = hotelService.getMettingRoomByHotel(hotelId);
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, diningRoomCustom);
	}
	
	@RequestMapping("/dining/add")
	public ApiResponse addDiningRoom(DiningRoomCustom diningRoom,@RequestParam MultipartFile[] diningImages) throws Exception{
		if (hotelService.getDiningRoomById(diningRoom.getObjectId()) == null) {
			hotelService.add(diningRoom);
		}
		if (diningImages != null && diningImages.length > 0) {
			for (MultipartFile hotelImage : diningImages) {
				String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				if (!hotelImage.isEmpty()) {
					 String originalName = hotelImage.getOriginalFilename();
					 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
					 String path = firstKey + "/hotel/" + datePath + "/" + fileName;
					 ossClient.putObject(bucketName, path, hotelImage.getInputStream());
					 Image image = new Image();
					 image.setObjectId(IdUtils.uuid());
					 image.setOrganizationId(diningRoom.getHoutelId());
					 image.setType(ConstantUtils.ORGANIZE_TYPE_HOTEL);
					 image.setSubtype(ConstantUtils.IMAGE_SUBTYPE_HOTEL_DINING);
					 image.setUrl(ossurl+path);
					 image.setCreatedAt(new Date());
					 image.setUpdatedAt(image.getCreatedAt());
					 imageService.add(image);
					 diningRoom.getImages().add(image);
					 logger.info("images url--->"+image.getUrl());
				}
			}
		}else{
			logger.info("images is null");
		}
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK,diningRoom);
	}
	
	@RequestMapping("/protocal/add")
	public ApiResponse addProtocal(Protocal protocal,@RequestParam MultipartFile image) throws Exception{
		if (!image.isEmpty()) {
			String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String originalName = image.getOriginalFilename();
			 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
			 String path = firstKey + "/hotel/" + datePath + "/" + fileName;
			 ossClient.putObject(bucketName, path, image.getInputStream());
			 protocal.setUrl(ossurl+path);
		}
		if (StringUtils.isEmpty(protocal.getObjectId())) {
			protocal.setObjectId(IdUtils.uuid());
			protocal.setCreatedAt(new Date());
			hotelService.add(protocal);
		}else {
			protocal.setUpdatedAt(new Date());
			hotelService.update(protocal);
		}
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, protocal);
	}
	
	@RequestMapping("/roomPrice/add")
	public ApiResponse addRoomPrice(@RequestBody JSONArray jsonArray) {
	    List<RoomPrice> roomPrices = JSON.parseArray(jsonArray.toJSONString(), RoomPrice.class);
		for (RoomPrice roomPrice : roomPrices) {
			if (StringUtils.isEmpty(roomPrice.getObjectId())) {
				roomPrice.setObjectId(IdUtils.uuid());
				hotelService.add(roomPrice);
			}else{
				hotelService.update(roomPrice);
			}
		}
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, roomPrices);
	}
	
	@RequestMapping("/dining/update")
	public ApiResponse updateDiningRoom(DiningRoomCustom diningRoom,@RequestParam MultipartFile[] diningImages) throws Exception{
		hotelService.update(diningRoom);
		if (diningImages != null && diningImages.length > 0) {
			for (MultipartFile hotelImage : diningImages) {
				String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				if (!hotelImage.isEmpty()) {
					 String originalName = hotelImage.getOriginalFilename();
					 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
					 String path = firstKey + "/hotel/" + datePath + "/" + fileName;
					 ossClient.putObject(bucketName, path, hotelImage.getInputStream());
					 Image image = new Image();
					 image.setObjectId(IdUtils.uuid());
					 image.setOrganizationId(diningRoom.getHoutelId());
					 image.setType(ConstantUtils.ORGANIZE_TYPE_HOTEL);
					 image.setSubtype(ConstantUtils.IMAGE_SUBTYPE_HOTEL_DINING);
					 image.setUrl(ossurl+path);
					 image.setCreatedAt(new Date());
					 image.setUpdatedAt(image.getCreatedAt());
					 imageService.add(image);
					 diningRoom.getImages().add(image);
					 logger.info("images url--->"+image.getUrl());
				}
			}
		}else{
			logger.info("images is null");
		}
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK,diningRoom);
	}
	
	@RequestMapping("/metting/add")
	public ApiResponse addMettingRoom(MettingRoomCustom diningRoom,@RequestParam MultipartFile[] meetingImages) throws Exception{
		diningRoom.setObjectId(IdUtils.uuid());
		hotelService.add(diningRoom);
		if (meetingImages != null && meetingImages.length > 0) {
			for (MultipartFile hotelImage : meetingImages) {
				String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				if (!hotelImage.isEmpty()) {
					 String originalName = hotelImage.getOriginalFilename();
					 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
					 String path = firstKey + "/hotel/" + datePath + "/" + fileName;
					 ossClient.putObject(bucketName, path, hotelImage.getInputStream());
					 Image image = new Image();
					 image.setObjectId(IdUtils.uuid());
					 image.setOrganizationId(diningRoom.getHoutelId());
					 image.setType(ConstantUtils.ORGANIZE_TYPE_HOTEL);
					 image.setSubtype(ConstantUtils.IMAGE_SUBTYPE_HOTEL_MEETING);
					 image.setUrl(ossurl+path);
					 image.setCreatedAt(new Date());
					 image.setUpdatedAt(image.getCreatedAt());
					 imageService.add(image);
					 diningRoom.getImages().add(image);
					 logger.info("images url--->"+image.getUrl());
				}
			}
		}else{
			logger.info("images is null");
		}
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK,diningRoom);
	}
	
	@RequestMapping("/metting/update")
	public ApiResponse updateMettingRoom(MettingRoomCustom diningRoom,@RequestParam MultipartFile[] meetingImages) throws Exception{
		hotelService.update(diningRoom);
		if (meetingImages != null && meetingImages.length > 0) {
			for (MultipartFile hotelImage : meetingImages) {
				String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				if (!hotelImage.isEmpty()) {
					 String originalName = hotelImage.getOriginalFilename();
					 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
					 String path = firstKey + "/hotel/" + datePath + "/" + fileName;
					 ossClient.putObject(bucketName, path, hotelImage.getInputStream());
					 Image image = new Image();
					 image.setObjectId(IdUtils.uuid());
					 image.setOrganizationId(diningRoom.getHoutelId());
					 image.setType(ConstantUtils.ORGANIZE_TYPE_HOTEL);
					 image.setSubtype(ConstantUtils.IMAGE_SUBTYPE_HOTEL_MEETING);
					 image.setUrl(ossurl+path);
					 image.setCreatedAt(new Date());
					 image.setUpdatedAt(image.getCreatedAt());
					 imageService.add(image);
					 diningRoom.getImages().add(image);
					 logger.info("images url--->"+image.getUrl());
				}
			}
		}else{
			logger.info("images is null");
		}
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK,diningRoom);
	}
	
	@RequestMapping("/imageuploader")
	public ApiResponse upload(@RequestParam MultipartFile[] images,String objectId) throws Exception{
		if (!StringUtils.isEmpty(objectId)) {
			logger.info("get hotelId --->"+objectId);
			if (images != null && images.length > 0) {
				for (MultipartFile hotelImage : images) {
					String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
					if (!hotelImage.isEmpty()) {
						 String originalName = hotelImage.getOriginalFilename();
						 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
						 String path = firstKey + "/hotel/" + datePath + "/" + fileName;
						 ossClient.putObject(bucketName, path, hotelImage.getInputStream());
						 Image image = new Image();
						 image.setObjectId(IdUtils.uuid());
						 image.setOrganizationId(objectId);
						 image.setType(ConstantUtils.ORGANIZE_TYPE_HOTEL);
						 image.setSubtype(ConstantUtils.IMAGE_SUBTYPE_HOTEL_INTRO);
						 image.setUrl(ossurl+path);
						 image.setCreatedAt(new Date());
						 image.setUpdatedAt(image.getCreatedAt());
						 imageService.add(image);
						 logger.info("hotelImages url--->"+image.getUrl());
					}
				}
			}
		}
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK);
	}

	@RequestMapping("/add")
	public ApiResponse addHotel(Hotel hotel,@RequestParam MultipartFile headerImage) throws Exception{
		hotel.setCreatedAt(new Date());
		hotel.setUpdatedAt(hotel.getCreatedAt());
		hotel.setUserId(currentUser().getObjectId());
		logger.info("hotel--->"+JSON.toJSONString(hotel));
		if (!headerImage.isEmpty()) {
			logger.info("headerImage is not null");
			String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			 String originalName = headerImage.getOriginalFilename();
			 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
			 String path = firstKey + "/hotel/" + datePath + "/" + fileName;
			 ossClient.putObject(bucketName, path, headerImage.getInputStream());
			 hotel.setImageUrl(ossurl+path);
			 logger.info("headerImage url--->"+hotel.getImageUrl());
		}else{
			logger.info("headerImage is empty");
		}
		hotelService.add(hotel);
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK,hotel);
	}
	
	@RequestMapping("/list")
	public ApiResponse list(Page page) {
		int counts = hotelService.count();
		page = PageUtils.createPage(page, counts);
		List<Hotel> hotels = hotelService.listHotel(page);
		HashMap<String, Object> map = new HashMap<>();
		map.put("hotels", hotels);
		map.put("page", page);
		return new ApiResponse(200, map);
	}
}
